package manong.serviceimpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import manong.beans.*;
import manong.constant.ServerResponse;
import manong.mapper.ClassroomMapper;
import manong.mapper.CourseTableMapper;
import manong.service.IClassroomService;
import manong.util.DayUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class ClassroomServiceImpl implements IClassroomService {

    @Autowired
    private ClassroomMapper classroomMapper;

    @Autowired
    private CourseTableMapper courseTableMapper;

    @Override
    public ServerResponse<String> add(String roomName, Integer roomSpace, Integer roomLayer, String mark) {
        ClassroomQuery query = new ClassroomQuery();
        query.createCriteria().andRoomNameEqualTo(roomName);
        Classroom classroom = classroomMapper.selectOneByExample(query);
        if (classroom != null) {
            return ServerResponse.createByErrorMessage("教室已经存在");
        }
        Classroom c = new Classroom();
        c.setRoomName(roomName);
        c.setRoomSpace(roomSpace);
        c.setRoomLayer(roomLayer);
        c.setMark(mark);
        int count = classroomMapper.insertSelective(c);
        if (count > 0) {
            return ServerResponse.createBySuccessMessage("新建成功");
        }
        return ServerResponse.createByErrorMessage("新建失败");
    }

    @Override
    public ServerResponse<String> del(Integer id) {
        int count = classroomMapper.deleteByPrimaryKey(id);
        if (count > 0) {
            return ServerResponse.createBySuccessMessage("删除成功");
        }
        return ServerResponse.createByErrorMessage("删除失败");
    }

    @Override
    public ServerResponse<Classroom> find(Integer id) {
        return ServerResponse.createBySuccess(classroomMapper.selectByPrimaryKey(id));
    }

    @Override
    public ServerResponse<PageInfo<Classroom>> findAll(Integer pageNum, Integer pageSize) {
        ClassroomQuery query = new ClassroomQuery();
        PageHelper.startPage(pageNum, pageSize);
        List<Classroom> list = classroomMapper.selectByExampleWithRowbounds(query, new RowBounds((pageNum - 1) * 10, pageSize));
        PageInfo<Classroom> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(classroomMapper.countByExample(query));
        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse<String> update(Integer id, String roomName, Integer roomSpace, Integer roomLaye,String mark) {
        Classroom classroom = classroomMapper.selectByPrimaryKey(id);
        classroom.setRoomName(roomName);
        classroom.setRoomSpace(roomSpace);
        classroom.setRoomLayer(roomLaye);
        classroom.setMark(mark);
        System.out.println(roomLaye);
        int count = classroomMapper.updateByPrimaryKeySelective(classroom);
        if (count > 0) {
            return ServerResponse.createBySuccessMessage("更新成功");
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }

    
    
    

    
    
    @Override
    public List<ClassroomFree> findByWeek(Integer week, String roomName) {
    	//创建教室查询对象   --》 ClassroomQuery是一个封装了查询条件等等一些功能的对象
    	
        ClassroomQuery query1 = new ClassroomQuery();
        ClassroomQuery.Criteria criteria1 = query1.createCriteria();
        
        //判断教室名称是否为空，如果不为空，则添加模糊查询条件
        if (roomName != null && !"".equals(roomName)){
            criteria1.andRoomNameLike("%" + roomName + "%"); //roomName当前名字前面和后面可以有0或多个字符
        }
        
        //*****从mapper里回来  --》查询所有教室
        List<Classroom> classrooms = classroomMapper.selectByExample(query1); //接着进行一个select的查询  --》classroomMapper.java

        List<ClassroomFree> classroomFrees = new ArrayList<>();
        /*8888888888888888888888888
        
                     用于查询出所有教室
        
        
        88888888888888888888888*/
        
        //-----------------------------------------
        //*****查询出空的教室并放到classroomFree集合中
        for (Classroom classroom : classrooms) {
            int[][] arr = new int[5][4];  /*888888888 week，节  8888888*/
            Integer classroomId = classroom.getId();

            CourseTableQuery query = new CourseTableQuery();
            CourseTableQuery.Criteria criteria = query.createCriteria();
            criteria.andStatuEqualTo(week) //*****教室和week双字段去查询--》查询当前有课--》断点调试下一步--》查当前的数据库表course_table
                    .andRoomIdEqualTo(classroomId);
            List<CourseTable> courseTables = courseTableMapper.selectByExample(query);
/*8888888888888888888888888
    117-121        
            查询当前周一到周五有课的教室
 8888888888888888888888888 */          

            //88888888888888888第一个循环用于过滤出有课的教室
            for (CourseTable courseTable : courseTables) {
                Integer number = courseTable.getTimeId();
                //*****此为当前星期几
                String timeName = courseTable.getTimeName();
                arr[DayUtil.toDayNum(timeName) - 1][number - 1] = 1;//*****将当前roomid和week相加为1的--》当前有课的全部标记为1，没有课的为0
            }

            /*
         查出来以后（arr数组中      在周一到周五哪天有课，是4节中的哪个  ）  标记为1
             */
            
            //88888888888888888用于存放没课的空教室，并且将这个教室放入集合返回给前端
            //*****抽取出当前没有课的教室--》进行遍历
            for (int x = 1; x <= 5; x++) {//*****第一层为遍历星期一到星期五
                for (int y = 1; y <= 4; y++) {//*****第二层为每天遍历四次--》一天四节课
                    if (arr[x - 1][y - 1] == 0) {//*****当前为0的即没有被标记为1的
                        classroomFrees.add(toFree(classroom, DayUtil.toDay(x), y,week));//放入classroomFree集合当中，没有课的空教室放进去
                    }
                }
            }
        }
        return classroomFrees;   //*****返回给controller
    }
/*88888888888888888
112-149为第一层过滤-------》用于过滤出空教室----------》》》》回到controller看第二层的过滤
88888888888888888 */
    private ClassroomFree toFree(Classroom classroom, String day, Integer number,Integer week) {
        ClassroomFree classroomFree = new ClassroomFree();
        classroomFree.setClassroomId(classroom.getId());
        classroomFree.setClassroomName(classroom.getRoomName());
        classroomFree.setDay(day);
        classroomFree.setWeek(week);
        classroomFree.setNumber(number);
        return classroomFree;
    }
}
/*
以自己对空教室查询的总结---》简述：涉及到两个循环---》第一个循环用于过滤出有课的教室，用于存放没课的空教室，并且将这个教室放入集合返回给controller，controller返回给前端





*/